/*
* Copyright 2025 NXP
* NXP Proprietary. This software is owned or controlled by NXP and may only be used strictly in
* accordance with the applicable license terms. By expressly accepting such terms or by downloading, installing,
* activating and/or otherwise using the software, you are agreeing that you have read, and that you agree to
* comply with and are bound by, such license terms.  If you do not agree to be bound by the applicable license
* terms, then you may not retain, install, activate or otherwise use the software.
*/

#include "lvgl.h"
#include "gui_guider.h"
#include "widgets_init.h"
#include <stdlib.h>
#include "http_weather.h"
#include "ntp_time.h"

// 天气更新函数
void update_weather()
{
    char temperatureString_update[10];
    // 更新天气显示
    snprintf(temperatureString_update, sizeof(temperatureString_update), "%d℃", Intimate_weather.temperature);
    lv_label_set_text(guider_ui.screen_weather_temperature_data, temperatureString_update);
    lv_label_set_text(guider_ui.screen_weather_weather_data, Intimate_weather.weather_text);
    lv_label_set_text(guider_ui.screen_weather_location_data, Intimate_weather.location_name);
}

// 日期计算天数函数
int days_in_month(int year, int month)
{
    // 这里需要实现一个准确的算法来计算每个月的天数，包括闰年判断
    // 例如：
    switch (month)
    {
        case 2:
            return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) ? 29 : 28;
        case 4:
        case 6:
        case 9:
        case 11:
            return 30;
        default:
            return 31;
    }
}

// 日期更新函数
void update_date()
{
    // 全局变量，存储当前日期
    int current_year = time_info.year;
    int current_month = time_info.month;
    int current_day = time_info.day;
    
    // 更新日期变量
    current_day += 1;
    if (current_day > days_in_month(current_year, current_month))
    {
        current_day = 1;
        current_month += 1;
        if (current_month > 12)
        {
            current_month = 1;
            current_year += 1;
        }
    }

    time_info.year = current_year;
    time_info.month = current_month;
    time_info.day = current_day;

    // 更新日期显示
    char date_str[16];
    lv_snprintf(date_str, sizeof(date_str), "%d/%02d/%02d",current_year, current_month, current_day);
    lv_label_set_text(guider_ui.screen_datetext_1, date_str);
}


__attribute__((unused)) void kb_event_cb (lv_event_t *e) {
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *kb = lv_event_get_target(e);
    if(code == LV_EVENT_READY || code == LV_EVENT_CANCEL) {
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
    }
}

__attribute__((unused)) void ta_event_cb (lv_event_t *e) {
    lv_event_code_t code = lv_event_get_code(e);
#if LV_USE_KEYBOARD || LV_USE_ZH_KEYBOARD
    lv_obj_t *ta = lv_event_get_target(e);
#endif
    lv_obj_t *kb = lv_event_get_user_data(e);
    if (code == LV_EVENT_FOCUSED || code == LV_EVENT_CLICKED)
    {
#if LV_USE_ZH_KEYBOARD != 0
        lv_zh_keyboard_set_textarea(kb, ta);
#endif
#if LV_USE_KEYBOARD != 0
        lv_keyboard_set_textarea(kb, ta);
#endif
        lv_obj_move_foreground(kb);
        lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);
    }
    if (code == LV_EVENT_CANCEL || code == LV_EVENT_DEFOCUSED)
    {

#if LV_USE_ZH_KEYBOARD != 0
        lv_zh_keyboard_set_textarea(kb, ta);
#endif
#if LV_USE_KEYBOARD != 0
        lv_keyboard_set_textarea(kb, ta);
#endif
        lv_obj_move_background(kb);
        lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
    }
}

#if LV_USE_ANALOGCLOCK != 0
void clock_count(int *hour, int *min, int *sec)
{
    (*sec)++;
    if(*sec == 60)
    {
        *sec = 0;
        (*min)++;
    }
    if(*min == 60)
    {
        *min = 0;
        if(*hour < 12)
        {
            (*hour)++;
        } else {
            (*hour)++;
            *hour = *hour %12;
        }
    }
}
#endif

extern int screen_digital_clock_1_hour_value;
extern int screen_digital_clock_1_min_value;
extern int screen_digital_clock_1_sec_value;

void screen_digital_clock_1_timer(lv_timer_t *timer)
{
    // 更新时间
    clock_count_24(&screen_digital_clock_1_hour_value, &screen_digital_clock_1_min_value, &screen_digital_clock_1_sec_value);
    // 更新时间显示
    if (lv_obj_is_valid(guider_ui.screen_digital_clock_1))
    {
        lv_dclock_set_text_fmt(guider_ui.screen_digital_clock_1, "%d:%02d:%02d", screen_digital_clock_1_hour_value, screen_digital_clock_1_min_value, screen_digital_clock_1_sec_value);
    }

    // 检查是否是整点，更新天气
    if ((screen_digital_clock_1_hour_value == 0 || screen_digital_clock_1_hour_value == 6 || screen_digital_clock_1_hour_value == 12 || screen_digital_clock_1_hour_value == 18) && screen_digital_clock_1_min_value == 0 && screen_digital_clock_1_sec_value == 0)
    {
        // 天气更新函数
        update_weather();
    }

    // 检查是否是00:00:00，更新日期
    if (screen_digital_clock_1_hour_value == 0 &&
        screen_digital_clock_1_min_value == 0 &&
        screen_digital_clock_1_sec_value == 0)
    {
        // 日期更新函数
        update_date();
    }
}

static lv_obj_t * screen_datetext_1_calendar;

void screen_datetext_1_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * btn = lv_event_get_target(e);
    if(code == LV_EVENT_FOCUSED) {
        char * s = lv_label_get_text(btn);
        if(screen_datetext_1_calendar == NULL) {
            screen_datetext_1_init_calendar(btn, s);
        }
    }
}

void screen_datetext_1_init_calendar(lv_obj_t *obj, char * s)
{
    if (screen_datetext_1_calendar == NULL) {
        lv_obj_add_flag(lv_layer_top(), LV_OBJ_FLAG_CLICKABLE);
        screen_datetext_1_calendar = lv_calendar_create(lv_layer_top());
        lv_obj_t * scr = lv_obj_get_screen(obj);
        lv_coord_t scr_height = lv_obj_get_height(scr);
        lv_coord_t scr_width = lv_obj_get_width(scr);
        lv_obj_set_size(screen_datetext_1_calendar, scr_width * 0.8, scr_height * 0.8);
        char * year = strtok(s, "/");
        char * month = strtok(NULL, "/");
        char * day = strtok(NULL, "/");
        lv_calendar_set_showed_date(screen_datetext_1_calendar, atoi(year), atoi(month));
        lv_calendar_date_t highlighted_days[1];       /*Only its pointer will be saved so should be static*/
        highlighted_days[0].year = atoi(year);
        highlighted_days[0].month = atoi(month);
        highlighted_days[0].day = atoi(day);
        lv_calendar_set_highlighted_dates(screen_datetext_1_calendar, highlighted_days, 1);
        lv_obj_align(screen_datetext_1_calendar,LV_ALIGN_CENTER, 0, 0);

        lv_obj_add_event_cb(screen_datetext_1_calendar, screen_datetext_1_calendar_event_handler, LV_EVENT_ALL,NULL);
        lv_calendar_header_arrow_create(screen_datetext_1_calendar);
        lv_obj_update_layout(scr);
    }
}

void screen_datetext_1_calendar_event_handler(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t * obj = lv_event_get_current_target(e);

    if (code == LV_EVENT_VALUE_CHANGED) {
        lv_calendar_date_t date;
        lv_calendar_get_pressed_date(obj,&date);
        char buf[16];
        lv_snprintf(buf,sizeof(buf),"%d/%02d/%02d", date.year, date.month,date.day);
        lv_label_set_text(guider_ui.screen_datetext_1, buf);
        lv_obj_clear_flag(lv_layer_top(), LV_OBJ_FLAG_CLICKABLE);
        lv_obj_set_style_bg_opa(lv_layer_top(), LV_OPA_TRANSP, 0);
        lv_obj_del(screen_datetext_1_calendar);
        screen_datetext_1_calendar = NULL;
    }
}


const lv_img_dsc_t * screen_animimg_1_imgs[31] = {
    &screen_animimg_11,
    &screen_animimg_12,
    &screen_animimg_13,
    &screen_animimg_14,
    &screen_animimg_15,
    &screen_animimg_16,
    &screen_animimg_17,
    &screen_animimg_18,
    &screen_animimg_19,
    &screen_animimg_110,
    &screen_animimg_111,
    &screen_animimg_112,
    &screen_animimg_113,
    &screen_animimg_114,
    &screen_animimg_115,
    &screen_animimg_116,
    &screen_animimg_117,
    &screen_animimg_118,
    &screen_animimg_119,
    &screen_animimg_120,
    &screen_animimg_121,
    &screen_animimg_122,
    &screen_animimg_123,
    &screen_animimg_124,
    &screen_animimg_125,
    &screen_animimg_126,
    &screen_animimg_127,
    &screen_animimg_128,
    &screen_animimg_129,
    &screen_animimg_130,
    &screen_animimg_131,
};
